package cn.t.service.order;

import cn.t.constants.ParamConstants;
import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.core.service.ServiceImpl;
import cn.t.model.device.CargowayDO;
import cn.t.model.device.CargowaySkuDO;
import cn.t.model.order.ReplenishmentDetailDO;
import cn.t.repository.order.ReplenishmentDetailRepository;
import cn.t.service.device.CargowayService;
import cn.t.service.device.CargowaySkuService;
import cn.t.vo.order.CreateReplenishmentLineVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 补货订单明细表(ReplenishmentDetail)领域服务
 *
 * @author t
 * @since 2024-05-24 10:19:12
 */
@Slf4j
@Service
@AllArgsConstructor
public class ReplenishmentDetailService extends ServiceImpl<ReplenishmentDetailDO, ReplenishmentDetailRepository> {
    public List<ReplenishmentDetailDO> getReplenishmentDetailList(List<String> rsSns) {

        if (CollectionUtils.isEmpty(rsSns)) {
            return Collections.emptyList();
        }
        ReplenishmentDetailDO replenishmentDetailDTO = new ReplenishmentDetailDO();
        replenishmentDetailDTO.setRsSnList(rsSns);
       return repository.findByModel(replenishmentDetailDTO, ParamConstants.INT_0, Integer.MAX_VALUE, null, null);
    }

    public void addDetail( List<CreateReplenishmentLineVO> list,Long deviceId, String rsSn) {
        Map<Long, CargowaySkuDO> cargowaySkuMap = cargowaySkuService.getDeviceSkuList(Collections.singletonList(deviceId)).stream().
                collect(Collectors.toMap(CargowaySkuDO::getCargowayId, cargowaySkuDO -> cargowaySkuDO, (k1, k2) -> k1));
        Map<Integer, CargowayDO> cargowayMap = cargowayService.getCargowayList(Collections.singletonList(deviceId)).stream().
                collect(Collectors.toMap(CargowayDO::getCargowayNo, cargowayDO -> cargowayDO, (k1, k2) -> k1));
        List<ReplenishmentDetailDO> replenishmentDetailList = list.stream().map(lineVO -> {
            CargowayDO cargowayDO = cargowayMap.get(lineVO.getCargowayNo());
            if (cargowayDO == null) {
                log.error("补货单明细创建失败，货道信息不存在，货道No:{}", lineVO.getCargowayNo());
                throw new BizException(ResponseError.DATA_ERR, "补货单明细创建失败，货道信息不存在，货道No:{}".concat(String.valueOf(lineVO.getCargowayNo())));
            }
            CargowaySkuDO cargowaySkuDO = cargowaySkuMap.get(cargowayDO.getId());
            if (cargowaySkuDO == null || ObjectUtils.isEmpty(cargowaySkuDO.getSkuCode())) {
                log.error("补货单明细创建失败，货道信息不存在，或商品未上架，货道No:{}", lineVO.getCargowayNo());
                throw new BizException(ResponseError.DATA_ERR, "补货单明细创建失败，货道信息不存在或商品未上架，货道No:{}".concat(String.valueOf(lineVO.getCargowayNo())));
            }

            ReplenishmentDetailDO replenishmentDetailDO = new ReplenishmentDetailDO();
            replenishmentDetailDO.setRsSn(rsSn);
            replenishmentDetailDO.setCargowayNo(lineVO.getCargowayNo());
            replenishmentDetailDO.setSkuCode(cargowaySkuDO.getSkuCode());
            replenishmentDetailDO.setSkuName(cargowaySkuDO.getSkuName());
            replenishmentDetailDO.setRsQty(lineVO.getRsNum());
            return replenishmentDetailDO;
        }).collect(Collectors.toList());
        addBatchDetail(replenishmentDetailList);
    }
    public void addBatchDetail(List<ReplenishmentDetailDO> list) {
        repository.addBatchDetail(list);
    }
    public void updateBatchDetail(List<ReplenishmentDetailDO> list) {
        repository.updateBatchDetail(list);
    }
    @Resource
    private CargowaySkuService cargowaySkuService;
    @Resource
    private CargowayService cargowayService;
}
